
package vv.lines.view.swing;

import vv.lines.view.ViewTimer;
import vv.lines.view.ViewTimerListener;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import static javax.swing.SwingUtilities.invokeLater;
import static vv.lines.view.swing.utils.SwingLinesConstants.TIMER_DELAY;

/**
 *
 * @author vpotapenko
 */
public class SwingViewTimer implements ViewTimer {

    private Timer timer;
    private ViewTimerListener listener;

    public SwingViewTimer() {
        final Runnable task = new Runnable() {
            public void run() {
                dispatchTimer();
            }
        };
        timer = new Timer(TIMER_DELAY, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                invokeLater(task);
            }
        });
    }

    private void dispatchTimer() {
        if (listener != null) listener.dispatchTimer();
    }

    public void start() {
        timer.start();
    }

    public void setListener(ViewTimerListener l) {
        this.listener = l;
    }
}
